package ExerciseOne;
/*
 * 同步代码块的线程安全问题
 */
public class ThreadSafe2Test {
	public static void main(String[] args) {
		Window2 w1 = new Window2();
		Window2 w2 = new Window2();
		w1.setName("线程一");
		w2.setName("线程二");
		w1.start();
		w2.start();
	}

}


class Window2 extends Thread
{
	private static int ticket =100;
	private static Object obj = new Object();
	@Override
	public void run() {
		while(true)
		{	
			synchronized (obj) {
				if(ticket>0)
				{
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName()+":"+ticket);
					ticket--;
				}
				else
				{
					break;
				}
			}
		}
	}
}